home *** CD-ROM | disk | FTP | other *** search
- /* Viewer DA
- View large text files
-
- Originally written in MegaMax C™,
- converted to LightspeedC™ December '87
-
- by Steven R. Costenoble
- */
-
- #include <DeviceMgr.h>
- #include <ControlMgr.h>
- #include <MenuMgr.h>
- #include <WindowMgr.h>
- #include <FontMgr.h>
- #include <EventMgr.h>
- #include <ToolboxUtil.h>
- #include <DialogMgr.h>
- #include "FileDisplay.h"
- #include "LineFile.h"
-
- #define NULL 0L
-
- enum { Open, Prime, Control, Status, Close };
-
- #define OWNEDID(D) ( 0xc000 | ~(D)->dCtlRefNum << 5 )
-
- #define MENUBARHEIGHT 21
- #define H_INSET 25
- #define V_INSET 25
-
- #define genalert 0
-
- #define tabdialog 1
- #define tabitem 3
-
- #define aboutdialog 2
-
- #define openitem 1 /* menu items */
- #define setitem 3
- #define quititem 5
- #define aboutitem 7
-
- /* globals for ease & call-back functions: */
-
- ControlHandle v_sbar, h_sbar;
- FDHandle hFD;
- int font_width, ownedid;
- MenuHandle my_menu;
-
- void accopen();
- void accctl();
- void accclose();
-
- main( pb, dce, what )
- cntrlParam *pb;
- DCtlPtr dce;
- int what;
- {
- if ( dce->dCtlStorage == NULL ) {
- if ( what == Open )
- GenError( "\pProblem opening Viewer DA, quitting",
- "\p", "\p", "\p" );
- return 0;
- }
-
- switch ( what ) {
- case Open: accopen( dce ); break;
- case Control: accctl( dce, pb ); break;
- case Close: accclose( dce ); break;
- }
-
- return 0;
- }
-
- void movesbars( win )
- register WindowPtr win;
- {
- Rect goodrect;
- register ControlHandle sbar;
-
- sbar = v_sbar;
-
- HideControl( sbar );
- MoveControl( sbar, win->portRect.right - 15,
- win->portRect.top - 1 );
- SizeControl( sbar, 16,
- win->portRect.bottom - win->portRect.top - 13 );
- ShowControl( sbar );
- goodrect = (**sbar).contrlRect;
- ValidRect( &goodrect );
-
- sbar = h_sbar;
-
- HideControl( sbar );
- MoveControl( sbar, win->portRect.left - 1,
- win->portRect.bottom - 15 );
- SizeControl( sbar,
- win->portRect.right - win->portRect.left - 13, 16 );
- ShowControl( sbar );
- goodrect = (**sbar).contrlRect;
- ValidRect( &goodrect );
- }
-
- Boolean doopen( mywindow, tabs )
- WindowPeek mywindow;
- int tabs;
- {
- LFile file;
- char name[ 256 ];
- Rect text_bounds;
- GrafPtr oldport;
-
- LineFile( LF_Open, &file, name );
- if ( file.size == 0 ) return FALSE;
-
- FileDisplay( FD_Dispose, hFD );
-
- SetWTitle( mywindow, name );
-
- text_bounds = mywindow->port.portRect;
- text_bounds.right -= 15;
- text_bounds.bottom -= 15;
-
- FileDisplay( FD_New, &hFD, &file, &text_bounds, (long) tabs );
-
- GetPort( &oldport );
- SetPort( mywindow );
- InvalRect( &text_bounds );
- SetPort( oldport );
-
- SetCtlValue( h_sbar, 0 );
- SetCtlValue( v_sbar, 0 );
-
- return TRUE;
- }
-
- void accopen( dce )
- DCtlPtr dce;
- {
- register WindowPeek mywindow;
- GrafPtr oldport, wport;
- FontInfo info;
- Rect win_bounds;
-
- ownedid = OWNEDID( dce );
- dce->dCtlFlags |= dNeedGoodBye;
- dce->dCtlMenu = ownedid;
-
- if ( dce->dCtlWindow != NULL ) return;
-
- GetPort( &oldport );
-
- GetWMgrPort( &wport );
- win_bounds = wport->portRect;
- win_bounds.top += MENUBARHEIGHT;
- InsetRect( &win_bounds, H_INSET, V_INSET );
-
- mywindow = (WindowPeek) NewWindow( NULL, &win_bounds, "\pViewer",
- FALSE, documentProc + 8, -1L, TRUE, 0L );
-
- if ( mywindow == NULL ) {
- GenError( "\pProblem opening Viewer DA, quitting",
- "\p", "\p", "\p" );
- return;
- }
-
- mywindow->windowKind = dce->dCtlRefNum;
- dce->dCtlWindow = (WindowPtr) mywindow;
-
- SetPort( mywindow );
-
- TextFont( monaco );
- TextSize( 9 );
-
- GetFontInfo( &info );
- font_width = info.widMax;
-
- v_sbar = NewControl( mywindow, &win_bounds, "\p", FALSE, 0, 0, 99,
- scrollBarProc, 0L );
-
- h_sbar = NewControl( mywindow, &win_bounds, "\p", FALSE, 0, 0, 255,
- scrollBarProc, 0L );
-
- movesbars( mywindow );
-
- hFD = NULL;
-
- if ( !doopen( mywindow, 4 ) ) {
- DisposeWindow( mywindow );
- dce->dCtlWindow = NULL;
- return;
- }
-
- my_menu = NewMenu( ownedid, "\pViewer" );
- AppendMenu( my_menu, "\pOpen…;(-;Set Tabs…;(-;Quit;(-;About Viewer…" );
-
- SetPort( oldport );
-
- return;
- }
-
- void doshutdown( mywindow )
- WindowPeek mywindow;
- {
- FileDisplay( FD_Dispose, hFD );
- DisposeWindow( mywindow );
- HiliteMenu( 0 );
- DeleteMenu( ownedid );
- DisposeMenu( my_menu );
- DrawMenuBar();
- }
-
- void accclose( dce )
- DCtlPtr dce;
- {
- register WindowPtr mywindow;
-
- if ( ( mywindow = dce->dCtlWindow ) != NULL )
- doshutdown( mywindow );
-
- dce->dCtlWindow = NULL;
-
- return;
- }
-
- void dokeydown( theEvent )
- register EventRecord *theEvent;
- {
- if ( ( theEvent->modifiers & cmdKey ) &&
- ( ( theEvent->message & charCodeMask ) == 'c' ||
- ( theEvent->message & charCodeMask ) == 'C' ) )
- FileDisplay( FD_Copy, hFD );
- }
-
- void doactivate( mywindow, theEvent )
- WindowPeek mywindow;
- EventRecord *theEvent;
- {
- if ( theEvent->modifiers & activeFlag ) {
- ShowControl( v_sbar ); /* activate */
- ShowControl( h_sbar );
- FileDisplay( FD_Activate, hFD, 1L );
- InsertMenu( my_menu, 0 );
- }
-
- else {
- HideControl( v_sbar ); /* deactivate */
- HideControl( h_sbar );
- FileDisplay( FD_Activate, hFD, 0L );
- DeleteMenu( ownedid );
- }
-
- DrawGrowIcon( mywindow );
- DrawMenuBar();
- }
-
- void doupdate( mywindow )
- register WindowPtr mywindow;
- {
- BeginUpdate( mywindow );
-
- EraseRect( &mywindow->portRect );
- UpdtControl( mywindow, mywindow->visRgn );
- DrawGrowIcon( mywindow );
-
- FileDisplay( FD_Update, hFD );
-
- EndUpdate( mywindow );
- }
-
- void docursor( mywindow )
- WindowPtr mywindow;
- {
- Rect text_rect;
- Point where;
- register CursHandle icurs;
-
- text_rect = mywindow->portRect;
- text_rect.right -= 15;
- text_rect.bottom -= 15;
-
- GetMouse( &where );
- if ( PtInRect( where, &text_rect ) ) {
- icurs = GetCursor( iBeamCursor );
- SetCursor( *icurs );
- }
- else
- InitCursor();
-
- FileDisplay( FD_Idle, hFD );
- }
-
- void doset()
- {
- register DialogPtr dlog;
-
- int itemhit, itemtype;
- long newtab;
- Handle thetext;
- Rect itembox;
- char tabstr[ 256 ];
-
- dlog = GetNewDialog( ownedid + tabdialog, NULL, -1L );
-
- NumToString( (long) (**hFD).tabsize, tabstr );
-
- GetDItem( dlog, tabitem, &itemtype, &thetext, &itembox );
- SetIText( thetext, tabstr );
- SelIText( dlog, tabitem, 0, 32767 );
-
- ShowWindow( dlog );
- ModalDialog( NULL, &itemhit );
-
- if ( itemhit == OK ) {
- GetIText( thetext, tabstr );
- StringToNum( tabstr, &newtab );
- FileDisplay( FD_SetTabs, hFD, newtab );
- }
-
- DisposDialog( dlog );
- }
-
- void domenu( mident, item, mywindow )
- int mident, item;
- WindowPeek mywindow;
- {
- switch ( item ) {
- case openitem:
- doopen( mywindow, (**hFD).tabsize );
- break;
-
- case setitem:
- doset();
- break;
-
- case quititem:
- CloseDeskAcc( mywindow->windowKind );
- break;
-
- case aboutitem:
- Alert( ownedid + aboutdialog, NULL );
- break;
- }
- DrawMenuBar();
- }
-
- void dogrow( mywindow, where )
- register WindowPtr mywindow;
- Point where;
- {
- union { /* used to interpret growwindow return value */
- long retval;
- struct { int ht, wd; } dim;
- } newsize;
-
- Rect sizerect, textrect, goodrect;
-
- sizerect.top = sizerect.left = 70;
- sizerect.right = sizerect.bottom = 32000;
-
- if ( ( newsize.retval = GrowWindow( mywindow, where, &sizerect ) )
- != 0L ) {
- InvalRect( &mywindow->portRect );
- SizeWindow( mywindow, newsize.dim.wd, newsize.dim.ht, TRUE );
- movesbars( mywindow );
-
- goodrect = (**(**hFD).TE).viewRect;
-
- textrect = mywindow->portRect;
- textrect.right -= 15;
- textrect.bottom -= 15;
- FileDisplay( FD_Resize, hFD, &textrect );
-
- SectRect( &goodrect, &(**(**hFD).TE).viewRect, &goodrect );
- ValidRect( &goodrect );
- }
- }
-
- void dozoom( win, where, part )
- register WindowPtr win;
- Point where;
- int part;
- {
- Rect textrect;
-
- if ( TrackBox( win, where, part ) ) {
- EraseRect( &win->portRect );
- InvalRect( &win->portRect );
- ZoomWindow( win, part, FALSE );
- movesbars( win );
-
- textrect = win->portRect;
- textrect.right -= 15;
- textrect.bottom -= 15;
- FileDisplay( FD_Resize, hFD, &textrect );
- }
- }
-
- pascal v_action( sbar, part )
- ControlHandle sbar;
- int part;
- {
- long pos;
- int oldpercent, newpercent;
- register FDHandle HFD;
-
- SetUpA4();
-
- HFD = hFD;
-
- oldpercent = GetCtlValue( sbar );
- pos = (**HFD).filestarts[ 0 ];
-
- switch ( part ) {
-
- case inUpButton:
- if ( pos == 0L ) break;
- FileDisplay( FD_Scroll, HFD, 0L, 1L, &pos );
- break;
-
- case inDownButton:
- if ( (**HFD).actuallines < (**HFD).maxlines ) break;
- FileDisplay( FD_Scroll, HFD, 0L, -1L, &pos );
- break;
-
- case inPageUp:
- if ( pos > 0L )
- FileDisplay( FD_Redraw, HFD, &pos,
- (long) -(**HFD).maxlines + 1 );
- break;
-
- case inPageDown:
- if ( (**HFD).actuallines == (**HFD).maxlines ) {
- pos = (**HFD).filestarts[ (**HFD).actuallines - 1 ];
- FileDisplay( FD_Redraw, HFD, &pos, 0L );
- }
- break;
- }
-
- newpercent = ( pos * 100 ) / (**HFD).file.size;
- if ( newpercent != oldpercent )
- SetCtlValue( sbar, newpercent );
-
- RestoreA4();
- }
-
- void doVscroll( part, where )
- int part;
- Point where;
- {
- register int percent;
- long pos;
- register ControlHandle sbar = v_sbar;
-
- if ( part == inThumb ) {
- if ( TrackControl( sbar, where, NULL ) ) {
- percent = GetCtlValue( sbar );
- pos = ( (**hFD).file.size * percent ) / 100;
- FileDisplay( FD_Redraw, hFD, &pos, 0L );
- percent = ( pos * 100 ) / (**hFD).file.size;
- SetCtlValue( sbar, percent );
- }
- }
- else
- TrackControl( sbar, where, v_action );
- }
-
- pascal h_action( sbar, part )
- register ControlHandle sbar;
- int part;
- {
- register int oldcharpos, newcharpos;
- register long pixmove;
- register TEHandle hTE;
- long dummy;
-
- SetUpA4();
-
- hTE = (**hFD).TE;
-
- newcharpos = oldcharpos = GetCtlValue( sbar );
-
- switch ( part ) {
-
- case inUpButton:
- if ( oldcharpos > 0 )
- newcharpos -= 1;
- break;
-
- case inDownButton:
- if ( oldcharpos < GetCtlMax( sbar ) )
- newcharpos += 1;
- break;
-
- case inPageUp:
- newcharpos -= 40;
- if ( newcharpos < 0 )
- newcharpos = 0;
- break;
-
- case inPageDown:
- newcharpos += 40;
- if ( newcharpos > GetCtlMax( sbar ) )
- newcharpos = GetCtlMax( sbar );
- break;
- }
-
- pixmove = (**hTE).viewRect.left - (**hTE).destRect.left
- - newcharpos * font_width + BORDER;
- if ( pixmove )
- FileDisplay( FD_Scroll, hFD, pixmove, 0L, &dummy );
- if ( newcharpos != oldcharpos )
- SetCtlValue( sbar, newcharpos );
-
- RestoreA4();
- }
-
- void doHscroll( part, where )
- int part;
- Point where;
- {
- register long pixmove;
- register TEHandle hTE = (**hFD).TE;
- register ControlHandle sbar = h_sbar;
- long dummy;
-
- if ( part == inThumb ) {
- if ( TrackControl( sbar, where, NULL ) ) {
- pixmove = (**hTE).viewRect.left - (**hTE).destRect.left
- - GetCtlValue( sbar ) * font_width + BORDER;
- FileDisplay( FD_Scroll, hFD, pixmove, 0L, &dummy );
- }
- }
- else
- TrackControl( sbar, where, h_action );
- }
-
- void docontent( mywindow, theEvent )
- WindowPtr mywindow;
- EventRecord *theEvent;
- {
- ControlHandle sbar;
- register int part;
- Point where;
-
- where = theEvent->where;
- GlobalToLocal( &where );
-
- part = FindControl( where, mywindow, &sbar );
-
- if ( part != 0 )
- if ( sbar == v_sbar )
- doVscroll( part, where );
- else
- doHscroll( part, where );
- else
- FileDisplay( FD_Click, hFD, theEvent );
- }
-
- void domousedown( mywindow, theEvent )
- register WindowPeek mywindow;
- register EventRecord *theEvent;
- {
- WindowPtr whichwindow;
- int partcode, kind;
-
- kind = mywindow->windowKind;
- mywindow->windowKind = userKind; /* fool findwindow */
-
- switch ( partcode = FindWindow( theEvent->where, &whichwindow ) ) {
-
- case inContent:
- docontent( mywindow, theEvent );
- break;
-
- case inGrow:
- dogrow( mywindow, theEvent->where );
- break;
-
- case inZoomIn:
- case inZoomOut:
- dozoom( mywindow, theEvent->where, partcode );
- break;
- }
-
- mywindow->windowKind = kind;
- }
-
- void accctl( dce, pb )
- DCtlPtr dce;
- cntrlParam *pb;
- {
- register EventRecord *theEvent;
- register WindowPtr mywindow;
- GrafPtr oldport;
-
- if ( ( mywindow = dce->dCtlWindow ) == NULL ) return;
- GetPort( &oldport );
- SetPort( mywindow );
-
- switch ( pb->csCode ) {
-
- case accEvent:
- theEvent = *(EventRecord **) pb->csParam;
-
- switch ( theEvent->what ) {
-
- case mouseDown:
- domousedown( mywindow, theEvent );
- break;
-
- case keyDown:
- case autoKey:
- dokeydown( theEvent );
- break;
-
- case activateEvt:
- doactivate( mywindow, theEvent );
- break;
-
- case updateEvt:
- doupdate( mywindow );
- break;
- }
- break;
-
- case accCursor:
- docursor( mywindow );
- break;
-
- case accMenu:
- domenu( pb->csParam[0], pb->csParam[1], mywindow );
- break;
-
- case accCopy:
- FileDisplay( FD_Copy, hFD );
- break;
-
- case goodBye:
- doshutdown( mywindow );
- break;
- }
-
- SetPort( oldport );
- return;
- }
-
- GenError( str1, str2, str3, str4 )
- char str1[], str2[], str3[], str4[];
- {
- ParamText( str1, str2, str3, str4 );
- StopAlert( ownedid + genalert, NULL );
- }
-